package com.atjinggu.jinggumail.ware.listener;

import com.atjinggu.jinggumail.common.to.mq.OrderTo;
import com.atjinggu.jinggumail.common.to.mq.StockLockedTo;
import com.atjinggu.jinggumail.ware.service.WareSkuService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author jingming
 * @Type StockReleaseListener.java
 * @Desc
 * @date 2021/3/17 11:47
 */
@RabbitListener(queues = "stock.release.stock.queue")
@Component
public class StockReleaseListener {
    @Autowired
    WareSkuService wareSkuService;
    @RabbitHandler
    public void releaseStock(StockLockedTo to, Message message, Channel channel) throws IOException {
        message.getMessageProperties().getRedelivered();
        System.out.println("收到消息订单延迟消息----开始处理解锁库存");
        try {
            wareSkuService.releaseStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }


    @RabbitHandler
    public void releaseStock(OrderTo to, Message message, Channel channel) throws IOException {
        System.out.println("收到消息订单直发消息----开始处理解锁库存");
        try {
            wareSkuService.releaseStock(to);
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e){
            channel.basicReject(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}